package com.fubon.wloan.util;

import java.io.IOException;
import java.util.Base64;

import com.alibaba.fastjson.JSON;
import com.fubon.wloan.pojo.Trans;

public class CryptoUtil {

    //接收报文公钥，私钥在对方手里
    private static String publicKeyPath1 = "/Users/HY_LOAN/Desktop/rsakey/publicKey.keystore";
    //发送报文私钥，公钥给对方使用
    private static String publicKeyPath2 = "/Users/HY_LOAN/Desktop/rsakey/publicKey2.keystore";
    private static String privateKeyPath2 = "/Users/HY_LOAN/Desktop/rsakey/privateKey2.keystore";

    public static Trans decode(Trans msg) {

        try {
            String sign = msg.getSign();
            msg.setSign(null);
            if (!RSAUtils.verify(JSON.toJSONString(msg), sign, publicKeyPath2, true)) {
                return null;
            }
            msg.setKey(new String(
                    RSAUtils.decrypt(RSAUtils.loadPublicKey(publicKeyPath2), RSAUtils.base64Decode(msg.getKey()))));
            msg.setSalt(new String(
                    RSAUtils.decrypt(RSAUtils.loadPublicKey(publicKeyPath2), RSAUtils.base64Decode(msg.getSalt()))));
            msg.setData(AESOperator.decrypt(msg.getData(), msg.getKey(), msg.getSalt()));

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return msg;
    }

    public static Trans encode(Trans msg) {
        try {
            String salt = RSAUtils.getRandomString(16);
            String key = RSAUtils.getRandomString(16);
            String salt_encrypt = Base64.getEncoder()
                    .encodeToString(RSAUtils.encrypt(RSAUtils.loadPrivateKey(privateKeyPath2), salt));
            String key_encrypt = Base64.getEncoder()
                    .encodeToString(RSAUtils.encrypt(RSAUtils.loadPrivateKey(privateKeyPath2), key));
            msg.setData(AESOperator.encrypt(msg.getData(), key, salt));
            msg.setKey(key_encrypt);
            msg.setSalt(salt_encrypt);
            msg.setSign(RSAUtils.sign(JSON.toJSONString(msg), privateKeyPath2, null));
            msg.setId(null);
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return msg;
    }

    public static void main(String[] args) {
        publicKeyPath1 = "/Users/HY_LOAN/Desktop/rsakey/publicKey2.keystore";
        Trans trans = new Trans();
        trans.setIcode("xxxx");
        trans.setData("hello world");
        System.out.println("----------------------原始报文----------------------------------------------");
        System.out.println(JSON.toJSONString(trans));
        Trans msg = encode(trans);
        System.out.println("----------------------加密报文----------------------------------------------");
        System.out.println(JSON.toJSONString(msg));
        Trans msg2 = decode(msg);
        System.out.println("----------------------解密报文----------------------------------------------");
        System.out.println(JSON.toJSONString(msg2));
    }

}